/*     */ package com.gopay.bis.gdb.ebank.gfbank.payment.merchant;
/*     */ 
/*     */ import java.io.File;
/*     */ import java.io.FileOutputStream;
/*     */ import java.io.PrintStream;
/*     */ import java.security.MessageDigest;
/*     */ import java.security.PrivateKey;
/*     */ import java.security.PublicKey;
/*     */ import java.security.Signature;
/*     */ 
/*     */ public class SignAndVerify
/*     */ {
/*  11 */   static String orderid_path = "";
/*  12 */   static int orderid_length = 0;
/*  13 */   static String merchantprivatekey = "";
/*  14 */   static String gdbpublickey = "";
/*  15 */   static boolean proploaded = false;
/*  16 */   static String merchantid = "";
/*     */ 
/*     */   public static boolean loadprop()
/*     */     throws Exception
/*     */   {
/*     */     try
/*     */     {
/*  25 */       if (proploaded) {
/*  26 */         return true;
/*     */       }
/*  28 */       merchantid = PayCfg.getValue("merchid");
/*  29 */       orderid_path = PayCfg.getValue("orderid_path");
/*  30 */       orderid_length = Integer.parseInt(PayCfg.getValue("orderid_length"));
/*  31 */       merchantprivatekey = PayCfg.getValue("merchantprivatekey");
/*  32 */       gdbpublickey = PayCfg.getValue("gdbpublickey");
/*     */ 
/*  34 */       proploaded = true;
/*     */     } catch (Exception e) {
/*  36 */       System.err.println("in SignAndVerify.loadprop()  Caught exception " + e.toString());
/*  37 */       throw e;
/*     */     }
/*  39 */     return true;
/*     */   }
/*     */ 
/*     */   public static String byteToHex(byte[] abyte0)
/*     */   {
/*  44 */     StringBuffer stringbuffer = new StringBuffer();
/*  45 */     for (int i = 0; i < abyte0.length; i++)
/*     */     {
/*  47 */       String s = Integer.toHexString(abyte0[i] & 0xFF);
/*  48 */       if (s.length() != 2)
/*  49 */         stringbuffer.append('0').append(s);
/*     */       else {
/*  51 */         stringbuffer.append(s);
/*     */       }
/*     */     }
/*  54 */     return new String(stringbuffer);
/*     */   }
/*     */ 
/*     */   public static byte[] hexToByte(String s)
/*     */   {
/*  59 */     int i = s.length() / 2;
/*  60 */     byte[] abyte0 = new byte[i];
/*  61 */     for (int j = 0; j < i; j++)
/*     */     {
/*  63 */       String s1 = s.substring(j * 2, j * 2 + 2);
/*  64 */       abyte0[j] = (byte)Integer.parseInt(s1, 16);
/*     */     }
/*     */ 
/*  67 */     return abyte0;
/*     */   }
/*     */ 
/*     */   public static void main(String[] args)
/*     */   {
/*  72 */     String signdata = "002100121400022123456789012341";
/*     */     try
/*     */     {
/*  75 */       System.out.println(sign_md(signdata, ""));
/*  76 */       return;
/*     */     }
/*     */     catch (Exception _ex)
/*     */     {
/*     */     }
/*     */   }
/*     */ 
/*     */   public static void packetSign(String[] as)
/*     */     throws Exception
/*     */   {
/*  87 */     String s = sign_md(as[1], as[0]);
/*  88 */     as[3] = s;
/*  89 */     System.out.println(s);
/*     */   }
/*     */ 
/*     */   public static void packetVerify(String[] as)
/*     */     throws Exception
/*     */   {
/* 105 */     as[3] = verify_md(as[1], as[2], as[0]);
/*     */   }
/*     */ 
/*     */   public static String sign(String s, String s1)
/*     */     throws Exception
/*     */   {
/* 111 */     String s2 = null;
/*     */     try
/*     */     {
/* 114 */       PrivateKey privatekey = KeyManager.getPrivateKey(s1);
/* 115 */       Signature signature = Signature.getInstance("SHA/DSA");
/* 116 */       signature.initSign(privatekey);
/* 117 */       signature.update(s.getBytes());
/* 118 */       byte[] abyte0 = signature.sign();
/* 119 */       System.out.println("sig:" + abyte0.length);
/* 120 */       s2 = byteToHex(abyte0);
/*     */     }
/*     */     catch (Exception exception)
/*     */     {
/* 124 */       System.err.println("Exception in sign:" + exception.toString());
/* 125 */       throw new Exception();
/*     */     }
/* 127 */     return s2;
/*     */   }
/*     */ 
/*     */   public static String sign_md(String s, String s1)
/*     */     throws Exception
/*     */   {
/* 133 */     String s2 = null;
/*     */     try
/*     */     {
/* 136 */       int le = merchantid.length();
/* 137 */       //if ((s == null) || (s.length() <= le + orderid_length))
/*     */      // {
/* 139 */         //System.err.println("Error:in sign_md:innuf is null or lack param. s.length=" + s.length() + "le=" + le + "orderid_length=" + orderid_length);
/* 140 */         //return "6";
/*     */       //}
/*     */       try
/*     */       {
/* 144 */         loadprop();
/*     */       } catch (Exception e) {
/* 146 */         System.err.println("in SignAndVerify.sign_md()  Caught exception " + e.toString());
/* 147 */         return "9";
/*     */       }
/*     */ 
/*     */       try
/*     */       {
/* 152 */         PrivateKey privatekey = KeyManager.getPrivateKey(merchantprivatekey);
/* 153 */         Signature signature = Signature.getInstance("DSA");
/* 154 */         signature.initSign(privatekey);
/* 155 */         MessageDigest messagedigest = MessageDigest.getInstance("SHA");
/* 156 */         byte[] abyte0 = messagedigest.digest(s.getBytes());
/*     */ 
/* 158 */         signature.update(abyte0);
/* 159 */         byte[] abyte1 = signature.sign();
/* 160 */         s2 = byteToHex(abyte1);
/*     */       } catch (Exception e) {
/* 162 */         return "1";
/*     */       }
/*     */ 
/* 175 */       //FileOutputStream f = new FileOutputStream(orderid_path + "/" + s.substring(le, le + orderid_length));
/* 176 */       //f.close();
/*     */     }
/*     */     catch (Exception exception)
/*     */     {
/* 183 */       System.err.println("Exception in sign_md:" + exception.toString());
/* 184 */       exception.printStackTrace();
/*     */ 
/* 186 */       return "4";
/*     */     }
/* 188 */     return s2;
/*     */   }
/*     */ 
/*     */   public static boolean verify(String s, String s1, String s2)
/*     */     throws Exception
/*     */   {
/*     */     try
/*     */     {
/* 196 */       if ((s == null) || (s1 == null) || (s2 == null))
/*     */       {
/* 198 */         System.err.println("Error:in verify:innuf or sign or filename is null");
/* 199 */         throw new Exception();
/*     */       }
/*     */ 
/* 202 */       PublicKey publickey = KeyManager.getPublicKey(s2);
/* 203 */       Signature signature = Signature.getInstance("SHA/DSA");
/* 204 */       signature.initVerify(publickey);
/* 205 */       signature.update(s.getBytes());
/* 206 */       byte[] abyte0 = hexToByte(s1);
/* 207 */       return signature.verify(abyte0);
/*     */     }
/*     */     catch (Exception exception)
/*     */     {
/* 212 */       System.err.println("Exception in verify:" + exception.toString());
/*     */     }
/* 214 */     throw new Exception();
/*     */   }
/*     */ 
/*     */   public static String verify_md(String s, String s1, String s2)
/*     */     throws Exception
/*     */   {
/*     */     try
/*     */     {
/* 222 */       if ((s == null) || (s1 == null))
/*     */       {
/* 224 */         System.err.println("Error:in verify_md:innuf or sign or filename is null");
/* 225 */         return "6";
/*     */       }
/*     */ 
/* 228 */       boolean ret = false;
/*     */       try
/*     */       {
/*     */         try {
/* 232 */           loadprop();
/*     */         } catch (Exception e) {
/* 234 */           return "9";
/*     */         }
/*     */ 
/* 237 */         PublicKey publickey = KeyManager.getPublicKey(gdbpublickey);
/* 238 */         Signature signature = Signature.getInstance("SHA/DSA");
/* 239 */         signature.initVerify(publickey);
/*     */ 
/* 241 */         MessageDigest messagedigest = MessageDigest.getInstance("SHA");
/* 242 */         byte[] abyte0 = messagedigest.digest(s.getBytes());
/* 243 */         signature.update(abyte0);
/* 244 */         byte[] abyte1 = hexToByte(s1);
/*     */ 
/* 246 */         ret = signature.verify(abyte1);
/*     */       }
/*     */       catch (Exception e) {
/* 249 */         return "3";
/*     */       }
/*     */ 
/* 252 */       if (ret)
/*     */       {
/*     */         //try {
/* 255 */           //File f = new File(orderid_path + "/" + s.substring(0, orderid_length));
/* 256 */           //f.delete(); } catch (Exception localException1) {
/*     */         //}
/* 258 */         return "0";
/*     */       }
/*     */ 
/* 261 */       return "7";
/*     */     }
/*     */     catch (Exception exception)
/*     */     {
/* 266 */       System.err.println("Exception in SignAndVerify.verify_md():" + exception.toString());
/*     */     }
/*     */ 
/* 269 */     return "4";
/*     */   }
/*     */ }

/* Location:           D:\桌面\广发银行\B2C支付签名验签工具包\JAVA版\gfgate\WEB-INF\classes\
 * Qualified Name:     gfbank.payment.merchant.SignAndVerify
 * JD-Core Version:    0.6.0
 */